package com.dfdchem.imd.kaiwu.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.dfdchem.imd.kaiwu.R;
import com.dfdchem.imd.kaiwu.activity.base.BaseActivityAnon;
import com.dfdchem.imd.kaiwu.constant.RouterPath;
import com.dfdchem.imd.kaiwu.interfaces.OnHttpResponseListener;
import com.dfdchem.imd.kaiwu.manager.DataManager;
import com.dfdchem.imd.kaiwu.model.AppConfig;
import com.dfdchem.imd.kaiwu.model.Resp;
import com.dfdchem.imd.kaiwu.model.User;
import com.dfdchem.imd.kaiwu.util.JSON;
import com.dfdchem.imd.kaiwu.util.Log;
import com.dfdchem.imd.kaiwu.util.RequestUtil;
import com.dfdchem.imd.kaiwu.util.StringUtil;
import com.dfdchem.imd.kaiwu.util.VersionUtil;

@Route(path = RouterPath.ENV_SETTING_ACTIVITY)
public class EnvSettingActivity extends BaseActivityAnon implements View.OnClickListener, OnHttpResponseListener {

    private static final String TAG = "EnvSettingActivity";

    private Button save;
    private Switch is_first_launch;
    private Switch is_internal;
    private Switch is_x5;
    private Switch is_test;
    private View layout_is_test;
    private View x5_setting;

    private final int request_code_get_config = 1;

    /**
     * 启动这个Activity的Intent
     * @param context
     * @return Intent
     */
    public static Intent createIntent(Context context) {
        Intent intent = new Intent(context, EnvSettingActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        initView();
        initData();
        initEvent();
    }

    @Override
    public void initView() {
        save = findView(R.id.save);
        is_first_launch = findView(R.id.is_first_launch);
        is_first_launch.setChecked(DataManager.getInstance().isFirstLaunch(VersionUtil.getAppVersionName(getApplicationContext())));

        is_internal = findView(R.id.is_internal);
        is_internal.setChecked(DataManager.getInstance().isIntranet());

        is_x5 = findView(R.id.is_x5);
        is_x5.setChecked(DataManager.getInstance().isX5());

        x5_setting = findView(R.id.x5_setting);
        User user = DataManager.getInstance().getUser();
        if (user != null && user.getId() == 100000) {
            x5_setting.setVisibility(View.VISIBLE);
        }

        layout_is_test = findView(R.id.layout_is_test);
        is_test = findView(R.id.is_test);
        is_test.setChecked(DataManager.getInstance().isTest());
    }

    @Override
    public void initData() {
        if (DataManager.getInstance().getUser() != null) {
            // 由于接口需要登录。所以导致未登录从LoginActivity进入时，打开此页面一直重复显示需要登录
            runThread(TAG + "getConfigForTestMode", new Runnable() {
                @Override
                public void run() {
                    RequestUtil.getConfig("app_base_config_test_mode", request_code_get_config, EnvSettingActivity.this);
                }
            });
        }
    }

    @Override
    public void initEvent() {
        save.setOnClickListener(this);
        x5_setting.setOnClickListener(this);
        is_test.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.save:
                if (is_first_launch.isChecked()) {
                    DataManager.getInstance().clearIsLaunched(VersionUtil.getAppVersionName(getApplicationContext()));
                }

                DataManager.getInstance().setIsIntranet(is_internal.isChecked());
                DataManager.getInstance().setIsX5(is_x5.isChecked());
                DataManager.getInstance().setIsTest(is_test.isChecked());
                Log.d(TAG, DataManager.getInstance().isTest() + "");

                finish();
                break;
            case R.id.x5_setting:
                ARouter.getInstance()
                        .build(RouterPath.WEB_VIEW_X5_ACTIVITY)
                        .withString("url", "http://debugx5.qq.com")
                        .navigation(this);
                break;
        }
    }

    @Override
    public void onHttpResponse(int requestCode, String resultJson, Exception e) {
        switch (requestCode) {
            case request_code_get_config:
                Resp<AppConfig> appConfigResp = JSON.parseResp(resultJson, AppConfig.class);
                if (appConfigResp != null && appConfigResp.getData() != null) {
                    AppConfig appConfig = appConfigResp.getData();
                    if (!StringUtil.isEmpty(appConfig.getValue()) && appConfig.getValue().equals("true")) {
                        layout_is_test.setVisibility(View.VISIBLE);
                    }
                }
                break;
        }
    }
}
